<template>
  <div class="search">
    <App-header>
      <img slot="title" @click="prev" src="../assets/上一步.png" alt="">
    </App-header>
    <div class="shopSearch">
      <input ref="refSearch" type="search" v-model="inpSearch" placeholder="搜索商家、商品名称">
      <button @click="btnSearch('')">搜索</button>
    </div>
    <div class="searchHistory">
      <div class="searchHistory__title">
        <span>历史搜索</span>
        <img src="../assets/删 除.png" alt="">
      </div>
      <div @click="btnHistory" class="searchHistory__item" v-for="(item, index) in searchHistory" :key="index">
        {{item}}
      </div>
    </div>
    <div class="searchHistory searchHot">
        <div class="searchHistory__title">
          <span>热门搜索</span>
        </div>
        <div @click="searchHot" class="searchHistory__item" v-for="(item, index) in searchHotList" :key="index">
          {{item}}
        </div>
      </div>
    <div class="searchList">
      <div class="searchList__item" v-for="(item, index) in inpSearchList" :key="index">
        <router-link class="router_link" :to="{ name: 'ShoppingCart'}">
          <p>{{item.name}}</p>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import AppHeader from './AppHeader.vue'
import {message} from './co-message'

export default {
  name: 'Search',
  data () {
    return {
      geohash: this.$route.query.geohash,
      inpSearch: '',
      inpSearchList: [],
      searchHistory: ['粥', '123'],
      searchHotList: ['古茗', '螺蛳粉', '炸鸡', '汉堡', '煲仔饭', '奶茶', '披萨', '蛋糕', '鸡排', '面包']
    }
  },
  components: { AppHeader },
  created () {
  },
  methods: {
    prev () {
      this.$router.go(-1)
    },
    initData () {
      // 获取搜索历史记录
      if (this.$storage.getStore('searchHistory')) {
        this.inpSearch = JSON.parse(this.$storage.getStore('searchHistory'))
      } else {
        this.inpSearch = []
      }
    },
    btnSearch (searchHistory) {
      if (this.inpSearch.length === 0) {
        message({
          message: '请输入搜索内容',
          type: 'error'
        })
      } else {
        this.$http.search({
          geohash: this.geohash,
          keyword: this.inpSearch
        }).then(res => {
          window.console.log(res.data)
          window.console.log(this.$route.query.geohash)
          this.inpSearchList = res.data
        }).catch(err => {
          window.console.log(err)
        })
      }
    },
    searchHot (e) {
      this.inpSearch = e.target.innerText
      // this.$refs.refSearch.value = e.target.innerText
    },
    btnHistory (e) {
      this.inpSearch = e.target.innerText
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang=scss>
  *{
    margin: 0px;
    padding: 0px;
  }
  .search{
    width: 100%;
    height: 1334px;
    background-color: #fff;
    .shopSearch{
      width: 90%;
      height: 104px;
      margin: 0px auto;
      display: flex;
      align-items: center;
      justify-content: space-between;
      input{
        width: 85%;
        color: #999;
        padding: 16px 16px 16px 60px;
        border-radius: 4px;
        background-color: #f5f5f5;
        box-sizing: border-box;
        font-size: 28px;
        border: none;
        outline: none;
        background-image: url('../assets/搜索.png');
        background-position: 22px center;
        background-repeat: no-repeat;
        background-size: 30px;
      }
      button{
        border: 1px solid #fff;
        background-color: #fff;
        outline: none;
        color: #333;
        font-size: 35px;
        font-weight: bold;
      }
    }
    .searchList{
      width: 100%;
      position: absolute;
      background-color: #fff;
      top: 200px;
      left: 0px;
      .searchList__item{
        width: 100%;
        height: 70px;
        line-height: 70px;
        overflow: hidden;
        padding: 10px 40px;
        border-bottom: 1px solid #e4e4e4;
        .router_link{
          color: #818181;
          font-size: 30px;
          text-decoration: none;
        }
      }
    }
    .searchHistory{
      width: 90%;
      margin: 0px auto;
      .searchHistory__title{
        width: 100%;
        height: 105px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        span{
          font-size: 33px;
          font-weight: bold;
          color: #666;
        }
        img{
          width: 30px;
        }
      }
      .searchHistory__item{
        margin: 5px 25px 10px 0px;
        display: inline-block;
        text-align: center;
        border-radius: 5px;
        padding: 20px 17px;
        box-sizing: border-box;
        background-color: #f7f7f7;
        color: #666;
      }
    }
  }
</style>
